/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidorped;

import Persistence.ConversationFactory;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * @author Micosil
 */
public class ConversationList {

    private List<Conversation> conversationList = new CopyOnWriteArrayList();
    private final ReadWriteLock conversationLock = new ReentrantReadWriteLock(true);

    public void invite(User inviter, User invited) {
        conversationLock.readLock().lock();
        try {
            for (Conversation oneConversation : conversationList) {
                if (oneConversation.getID() == inviter.getConversationID()) {
                    oneConversation.invite(invited);
                }
            }
        } finally {
            conversationLock.readLock().unlock();
        }
    }

    public void leaveConversation(User user) {
        conversationLock.writeLock().lock();
        try {
            for (Conversation oneConversation : conversationList) {
                if (oneConversation.getID() == user.getConversationID()) {
                    oneConversation.leave(user);
                    if (oneConversation.getUsers().isEmpty()) {
                        closeConversation(oneConversation);
                    }
                }
            }
        } finally {
            conversationLock.writeLock().unlock();
        }
    }

    private void closeConversation(Conversation oneConversation) {
        conversationLock.writeLock().lock();
        try {
            ConversationFactory.getConversationPersistence().storeConversation(oneConversation.retrieveHistoryUserList(), oneConversation.retrieveHistory(), oneConversation.retrieveCreationDate());
            conversationList.remove(oneConversation);
        } finally {
            conversationLock.writeLock().unlock();
        }
    }

    public Conversation getConversationByID(int id) {
        conversationLock.readLock().lock();
        Conversation result = null;
        try {
            for (Conversation oneconvo : conversationList) {
                if (oneconvo.getID() == id) {                    
                    result = oneconvo;
                    break;
                }
            }
        } finally {
            conversationLock.readLock().unlock();
            return result;
        }
    }

    public boolean addUserIfInvited(User user, Integer conversationToJoin) {
        conversationLock.writeLock().lock();
        boolean result = false;
        try {
            for (Conversation convo : conversationList) {
                if (convo.getID() == conversationToJoin) {
                    if (convo.isInvited(user)) {
                        convo.join(user);
                        user.setConversation(convo);
                        result = true;
                        break;
                    }                    
                }                
            }
        } finally {
            conversationLock.writeLock().unlock();            
            return result;
        }
    }

    public void addConversation(Conversation convo) {
        conversationLock.writeLock().lock();
        try {
            conversationList.add(convo);
        } finally {
            conversationLock.writeLock().unlock();
        }
    }
}
